let cryptico = require('node-cryptico');

class Crypto{
    constructor(){
        let publicKey = "";
        let rsakey = "";


        this.getPublicKey = function(){
            return publicKey;
        }

        this.setPublicKey = function( val ){
            publicKey = val;
        }

        this.getRsakey = function(){
            return rsakey;
        }

        this.setRsakey = function( val ){
            rsakey = val;
        }

        this.generatePublicKey();
    }

    //生成公钥
    generatePublicKey(){
        // The passphrase used to repeatably generate this RSA key.
        let PassPhrase = "fregments++Bybejamin@#$)"; 
        
        // The length of the RSA key, in bits.
        let Bits = 1024; 
        
        this.setRsakey(cryptico.generateRSAKey( PassPhrase, Bits ));
        
        let publicKeyString = cryptico.publicKeyString( this.getRsakey() );
        this.setPublicKey( publicKeyString );

        return this.getPublicKey();
    }

    //加密
    encryptContent( val ){
        return cryptico.encrypt( val , this.getPublicKey() );
    }

    //解密
    decryptContent( cipher ){
        return cryptico.decrypt( cipher, this.getRsakey() );
    }
}


let ProxyCreatCrypto = (function(){
    process.instance = null;
    return function(){
        if(process.instance){
            return process.instance
        }
        return process.instance = new Crypto();
    }
})();

module.exports = ProxyCreatCrypto;